package com.admin.qanda;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.util.HashMapBinder;
import com.vo.QnABoardVO;

public class AdminController extends MultiActionController {
	Logger logger = Logger.getLogger(AdminController.class);
	private AdminLogic adminLogic = null;
	public void setAdminLogic(AdminLogic adminLogic) {
		this.adminLogic = adminLogic;
	}
	public ModelAndView writeForm(HttpServletRequest req
            , HttpServletResponse res)
	{
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/write");
		return mav;
	}
	public ModelAndView login(HttpServletRequest req
			, HttpServletResponse res)
	{
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/login");
		return mav;
	}
	public void answer(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		HashMapBinder hmb = new HashMapBinder(req);
		Map<String,Object> pMap = new HashMap<String,Object>();
		hmb.bind(pMap);
		//adminLogic.delete(pMap);
		adminLogic.answer(pMap);
		adminLogic.answercheck(pMap);
		res.sendRedirect("./getBoardList.hy");
	}
	public void delete(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException
			{
		HashMapBinder hmb = new HashMapBinder(req);
		Map<String,Object> pMap = new HashMap<String,Object>();
		hmb.bind(pMap);
		adminLogic.delete(pMap);
			}
	public void write(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		logger.info("===[[AdminController boardInsert call]]===");
		HashMapBinder hmb = new HashMapBinder(req);
		Map<String,Object> pMap = new HashMap<String,Object>();
		hmb.bind(pMap);
		adminLogic.write(pMap);
		res.sendRedirect("./getBoardList.hy");
}		
public ModelAndView getBoardList(HttpServletRequest req, HttpServletResponse res)
{
logger.info("===[[AdminController getBoardList call]]===");
List<HashMap> bList = new ArrayList<HashMap>();
bList = adminLogic.getBoardList();
ModelAndView mav = new ModelAndView();
mav.setViewName("admin/qnaList");
mav.addObject("bList",bList);
return mav;
	}	
public ModelAndView boardDetail(HttpServletRequest req, HttpServletResponse res)	throws Exception
{
	logger.info("===[[AdminController boardDetail call]]===");
	ModelAndView mav = new ModelAndView();
	int seq = Integer.parseInt(req.getParameter("seq"));
	QnABoardVO qVO = adminLogic.boardDetail(seq);
	String gubun = req.getParameter("gubun");
	if("2".equals(gubun)){
	mav.setViewName("admin/qnaAnswer");
	mav.addObject("qVO",qVO);
	return mav;
	}
	if("3".equals(gubun)){
	mav.setViewName("admin/qnaRead");
	mav.addObject("qVO",qVO);
	return mav;
	}
	else{
	mav.setViewName("admin/qnaRead");
	mav.addObject("qVO",qVO);
	return mav;
		}
	}	
}
